<?php

use App\Http\Controllers\Admin\GuestController as AdminGuestController;
use App\Http\Controllers\Auth\DashboardController;
use App\Http\Controllers\Auth\GuestController;
use App\Http\Controllers\Auth\UserController;
use App\Http\Controllers\Auth\Thirdparty\OffiAccountController;
use App\Http\Controllers\Auth\Thirdparty\OffiAccountOwnController;
use App\Http\Controllers\Auth\Thirdparty\MiniProgramController;
use App\Http\Controllers\Thirdparty\CallbackController;
use Laravel\Passport\Http\Controllers\AccessTokenController;

Route::any('/auth/login', fn() => response()->json(['status' => false, 'message' => 'Use /oauth/token'], 401))
    ->name('login');

// 会员
Route::prefix('auth')->name('auth.')->group(function() {

    Route::controller(GuestController::class)->group(function () {
        Route::post('register', 'register')->middleware('throttle')->name('register');
        Route::post('logout', 'logout')->middleware('auth:user')->name('logout');
    });

    Route::middleware('auth:user')->group(function () {

        // 会员中心
        Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
        Route::resource('users', UserController::class)->only(['index', 'store', 'show', 'update', 'destroy']);

        // 小程序
        Route::controller(MiniProgramController::class)->group(function () {
            Route::get('open/{action}', 'open')->whereIn('action', ['have', 'sameentity'])->name('open.get');
            Route::post('account/{action}', 'account')->name('account.post');
            Route::get('component/{action}', 'component')->where('action', 'jscode2session')->name('component.get');
            Route::post('component/{action}', 'component')->name('component.post');
            Route::get('wxa/{action}', 'wxa')->where('action', 'getsearchstatus')->name('wxa.get');
            Route::post('wxa/{action}', 'wxa')->name('wxa.post');
            Route::post('express/{action}', 'express')->name('express.post');
            Route::post('wedata/{action}', 'wedata')->name('wedata.post');
        });

        // 公众号
        Route::controller(OffiAccountController::class)->group(function () {
            Route::post('offiaccount/{action}', 'offiaccount')->name('offiaccount.post');
            Route::get('invoice/{action}', 'invoice')->whereIn('action', ['getticket'])->name('invoice.get');
            Route::post('invoice/{action}', 'invoice')->name('invoice.post');
        });
        
        Route::post('invoice/own/{action}', [OffiAccountOwnController::class, 'invoice'])->name('invoice.post');
    });

});

// 管理
Route::prefix('admin')->name('admin.')->group(function() {

    // 登陆
    Route::post('login', [AccessTokenController::class, 'issueToken'])->middleware(['throttle', 'auth:admin'])->name('login');
    Route::post('logout', [AdminGuestController::class, 'logout'])->middleware('auth:admin')->name('logout');
    
    // 会员中心
    Route::middleware('auth:admin')->group(function () {

        // Route::post('/token/refresh', [
        //     'uses' => 'TransientTokenController@refresh',
        //     'as' => 'token.refresh',
        // ]);

        // Route::post('/authorize', [
        //     'uses' => 'ApproveAuthorizationController@approve',
        //     'as' => 'authorizations.approve',
        // ]);

        // Route::delete('/authorize', [
        //     'uses' => 'DenyAuthorizationController@deny',
        //     'as' => 'authorizations.deny',
        // ]);

        // Route::get('/tokens', [
        //     'uses' => 'AuthorizedAccessTokenController@forUser',
        //     'as' => 'tokens.index',
        // ]);

        // Route::delete('/tokens/{token_id}', [
        //     'uses' => 'AuthorizedAccessTokenController@destroy',
        //     'as' => 'tokens.destroy',
        // ]);

        Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
        Route::resource('users', UserController::class)->only(['index', 'store', 'show', 'update', 'destroy']);

    });
});

// 平台回调
Route::prefix('callback')->group(function() {
    Route::controller(CallbackController::class)->name('callback.')->group(function () {
        Route::post('component', 'component')->name('component');
        Route::post('biz/{appid}', 'biz')->where('appid', 'wx[0-9a-z]{16,32}')->name('biz');
    });
});
